package com.glory.dgcpcpda.adapter;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.EqpKeepSelectorListBean;

import java.util.List;

import androidx.annotation.Nullable;

public class EqpKeepCheckListAdapter extends BaseQuickAdapter<EqpKeepSelectorListBean, BaseViewHolder> {

    private boolean showEnable;

    public EqpKeepCheckListAdapter(@Nullable List<EqpKeepSelectorListBean> data,boolean showEnable) {
        super(R.layout.adapter_keep_check_list,data);
        this.showEnable = showEnable;
    }

    @Override
    protected void convert(BaseViewHolder helper, EqpKeepSelectorListBean item) {
        helper.setText(R.id.tvItem1,item.getName());
        helper.setText(R.id.tvItem2,item.getDescription());
        helper.setGone(R.id.cbOKLayout,true);
//        helper.setGone(R.id.cbQcLayout,true);
        helper.setGone(R.id.cbNGLayout,true);
        CheckBox cbOK = helper.getView(R.id.cbOK);
        CheckBox cbNG = helper.getView(R.id.cbNG);
        CheckBox checkBoxQc = helper.getView(R.id.checkBoxQc);
        cbOK.setChecked(item.getReserved1().equals("Y"));
        cbNG.setChecked(item.getReserved1().equals("N"));
        cbOK.setTag(helper.getAdapterPosition());
        cbNG.setTag(helper.getAdapterPosition());
        checkBoxQc.setTag(helper.getAdapterPosition());
        checkBoxQc.setChecked(item.getReserved9().equals("Y"));
        if (showEnable){
            if (!item.getReserved1().isEmpty() && item.getReserved1().equals("Y")){
                cbOK.setEnabled(false);
                cbNG.setEnabled(false);
            }else {
                cbOK.setEnabled(true);
                cbNG.setEnabled(true);
            }
        }else {
            cbOK.setEnabled(true);
            cbNG.setEnabled(true);
        }

        checkBoxQc.setEnabled(showEnable);
        cbOK.setOnClickListener(v -> {
            boolean isChecked = ((CheckBox) v).isChecked();
            if (isChecked) {
                item.setReserved1("Y");
                cbNG.setChecked(false);
            }else if (!cbNG.isChecked()){
                item.setReserved1("");
            }
        });
        cbNG.setOnClickListener(v -> {
            boolean isChecked = ((CheckBox) v).isChecked();
            if (isChecked) {
                item.setReserved1("N");
                cbOK.setChecked(false);
            }else if (!cbOK.isChecked()){
                item.setReserved1("");
            }
        });
        if (item.getReserved10().length() > 0 && !item.getReserved10().contains(";")){
            helper.setTextColor(R.id.tvItem1, Color.RED );
        }
        helper.setGone(R.id.etRemarks,true);
        EditText et = helper.getView(R.id.etRemarks);
        et.setEnabled(showEnable);
        if (et.getTag() instanceof TextWatcher) {
            et.removeTextChangedListener((TextWatcher) et.getTag());
        }
        et.setText(item.getComments());
        setEditListener(et,item);
    }

    private void setEditListener(EditText editText, final EqpKeepSelectorListBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setComments(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
